/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xsltdebuggerjmxremotedemo;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import mx4j.tools.adaptor.http.HttpAdaptor;
import mx4j.tools.adaptor.http.XSLTProcessor;
import xsltdebuggerjmxremotedemo.jmx.Simple;

/**
 *
 * @author doug
 */
public class XSLTDebuggerJMXRemoteDemo {

    private static final String NAME_PARAM = "name";
    public static final String SERVICE_MANAGER_MBEAN_NAME = "ServiceManager";

    private static final String ADAPTER_PREFIX = ":name=HttpAdaptor,port=";
    
    private static final Logger LOGGER = Logger.getLogger("demo");
    
    static {
        try // prime XSLTDebugger remote connection
        {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            factory = null;
            transformer = null;
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XSLTDebuggerJMXRemoteDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private static MBeanServer _server;
    
    private HttpAdaptor _console;
    
    private Simple _simple = new Simple();

    public XSLTDebuggerJMXRemoteDemo() {
        LOGGER.finest("Starting...");
        try {
            _console = new HttpAdaptor(9092);
            XSLTProcessor processor = new XSLTProcessor();
            //processor.setHost(getHost());

            processor.setFile(System.getProperty("user.dir") + File.separator + "mx4j");
            _console.setProcessor(processor);

            _server.registerMBean(_console, new ObjectName(ADAPTER_PREFIX + "9092"));
            _server.registerMBean(_simple, new ObjectName(_server.getDefaultDomain(), NAME_PARAM,
                    SERVICE_MANAGER_MBEAN_NAME));

            _console.start();

       } catch (InstanceAlreadyExistsException ex) {
            Logger.getLogger(XSLTDebuggerJMXRemoteDemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MBeanRegistrationException ex) {
            Logger.getLogger(XSLTDebuggerJMXRemoteDemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotCompliantMBeanException ex) {
            Logger.getLogger(XSLTDebuggerJMXRemoteDemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedObjectNameException ex) {
            Logger.getLogger(XSLTDebuggerJMXRemoteDemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
       }
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        _server = MBeanServerFactory.createMBeanServer();
        
        new XSLTDebuggerJMXRemoteDemo();
        // TODO code application logic here
    }
}
